﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FOModels.BlogNews;
using Bussiness.Language;
using FOModels.Language;
using Bussiness.BlogNews;
namespace FO.Controllers
{
    public class BlogNewsCategoryController : Controller
    {
        //
        // GET: /BlogNewsCategoryCategory/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult gettop3catebynews()
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            BlogNewsCategoryBLL cardbll = new BlogNewsCategoryBLL();
            List<BlogNewsCategoryModels> list = cardbll.gettop3catebynews(lang.LangID);
            return PartialView(list);
        }

        [Authorize(Roles = "8,9")]
        public ActionResult ManagerBlogNewsCategory()
        {
            return View();
        }

        public ActionResult GetBlogNewsCategoryNotParent()
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            BlogNewsCategoryBLL cardbll = new BlogNewsCategoryBLL();
            List<BlogNewsCategoryModels> list  = cardbll.ResposiBlogNewsCategoryManagerExView(lang.LangID);
            ViewBag.Lang = lang.LangID;
            return PartialView(list);
        }
        [Authorize(Roles = "8,9")]
        public JsonResult getBlogNewsCategoryJson()
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            BlogNewsCategoryBLL cardbll = new BlogNewsCategoryBLL();
            List<BlogNewsCategoryModels> list = new List<BlogNewsCategoryModels>();
            list = cardbll.getallBlogNewsCategory(lang.LangID);
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertBlogNewsCategory()
        {
            BlogNewsCategoryBLL cardbll = new BlogNewsCategoryBLL();
            LanguageBLL langbll = new LanguageBLL();
            List<BlogNewsCategoryModels> blognewscate = new List<BlogNewsCategoryModels>();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            blognewscate = cardbll.ResposiBlogNewsCategory(lang.LangID);
            ViewBag.getallBlogNewsCategoryList = new SelectList(blognewscate, "BlogNewsCategoryID", "BlogNewsCategoryName");

            return View();
        }
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertBlogNewsCategory(FO.Models.BlogNewsCategoryInsert card)
        {
            BlogNewsCategoryBLL cardbll = new BlogNewsCategoryBLL();
            LanguageBLL langbll = new LanguageBLL();
            List<BlogNewsCategoryModels> blognewscate = new List<BlogNewsCategoryModels>();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            blognewscate = cardbll.ResposiBlogNewsCategory(lang.LangID);
            ViewBag.getallBlogNewsCategoryList = new SelectList(blognewscate, "BlogNewsCategoryID", "BlogNewsCategoryName");
            if (ModelState.IsValid)
            {
                BlogNewsCategoryModels item = new BlogNewsCategoryModels();
                BlogNewsCategoryModels itemLang = new BlogNewsCategoryModels();
                item.BlogNewsCategoryName = card.BlogNewsCategoryName;
                item.Enable = card.Enable;
                item.ParentID = card.ParentID;
                cardbll.InsertBlogNewsCategory(item);
                BlogNewsCategoryModels savingItem = cardbll.GetTop1BlogNewsCategory();
                itemLang.BlogNewsCategoryName = card.BlogNewsCategoryNameLang;
                itemLang.Enable = card.Enable;
                itemLang.ParentID = card.ParentID;
                item.BlogNewsCategoryID = savingItem.BlogNewsCategoryID;
                itemLang.BlogNewsCategoryID = savingItem.BlogNewsCategoryID;
                cardbll.InsertBlogNewsCategoryLang(item, 1);
                cardbll.InsertBlogNewsCategoryLang(itemLang, 2);
                return RedirectToAction("ManagerBlogNewsCategory");
            }
            return View(card);
        }

        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdateBlogNewsCategory(int BlogNewsCategoryID)
        {
            BlogNewsCategoryBLL cardbll = new BlogNewsCategoryBLL();
            LanguageBLL langbll = new LanguageBLL();
            List<BlogNewsCategoryModels> com = new List<BlogNewsCategoryModels>();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            com = cardbll.ResposiBlogNewsCategory(lang.LangID);
            Models.BlogNewsCategory c = new Models.BlogNewsCategory();
            BlogNewsCategoryModels card = cardbll.getBlogNewsCategoryInfo(BlogNewsCategoryID, lang.LangID);
            ViewBag.getallBlogNewsCategoryList = new SelectList(com, "BlogNewsCategoryID", "BlogNewsCategoryName",c.ParentID);
            c.BlogNewsCategoryID = card.BlogNewsCategoryID;
            c.ParentID = card.ParentID ?? default(int);
            c.BlogNewsCategoryName = card.BlogNewsCategoryName;
            c.Enable = card.Enable;
            return View(c);
        }
        [ValidateInput(false)]
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdateBlogNewsCategory(FO.Models.BlogNewsCategory card)
        {

            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            BlogNewsCategoryBLL cardbll = new BlogNewsCategoryBLL();
            List<BlogNewsCategoryModels> com = new List<BlogNewsCategoryModels>();
            BlogNewsCategoryModels ca = new BlogNewsCategoryModels();
            com = cardbll.ResposiBlogNewsCategory(lang.LangID);
            ca = cardbll.getBlogNewsCategoryInfo(card.BlogNewsCategoryID, lang.LangID);
            ViewBag.getallBlogNewsCategoryList = new SelectList(com, "BlogNewsCategoryID", "BlogNewsCategoryName", ca.ParentID);
            if (ca != null)
            {
                ca.BlogNewsCategoryID = card.BlogNewsCategoryID;
                ca.ParentID = card.ParentID;
                ca.BlogNewsCategoryName = card.BlogNewsCategoryName;
                ca.Enable = card.Enable;
                cardbll.UpdateBlogNewsCategory(ca, lang.LangID);

                return RedirectToAction("ManagerBlogNewsCategory");
            }
            return View();
        }

        [HttpPost]
        [Authorize(Roles = "8,9")]
        public JsonResult DeleteBlogNewsCategory(int BlogNewsCategoryID)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            BlogNewsCategoryBLL bll = new BlogNewsCategoryBLL();
            BlogNewsCategoryModels card = new BlogNewsCategoryModels();
            card = bll.getBlogNewsCategoryInfo(BlogNewsCategoryID, lang.LangID);
            if (card != null)
            {
                bll.DeleteBlogNewsCategory(card.BlogNewsCategoryID);
                var result = new { Status = "OK" };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var result = new { Status = "Xóa thất bại" };
                return Json(result, JsonRequestBehavior.AllowGet);
            }


        }
    }
}
